﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Money.GUI
{
    public class CategoryComboBox : ComboBox
    {
        public CategoryComboBox()
            : base()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;

            if (Categories.CategoryList != null)
            {
                Categories.CategoryList.Updated += CategoryList_Updated;
                this.DataSource = Categories.CategoryList;
            }

            this.DisplayMember = "Name";
            this.ValueMember = "Id";
        }

        private void CategoryList_Updated(object sender, EventArgs e)
        {
            this.DataSource = null;
            this.DataSource = sender as Categories;
        }

        // TODO we should really tidy up the colours that we use so they are more generic default window colours rather than fixed specified colours
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            Categories categories = this.DataSource as Categories;
            SolidBrush drawBrush;

            if (this.Enabled)
            {
                e.DrawBackground();
            }

            if (categories != null)
            {
                if (e.Index >= 0)
                {
                    int categroryId = Categories.CategoryList.GetCategoryId(categories[e.Index].Name);

                    if (this.Enabled == false)
                    {
                        drawBrush = new SolidBrush(SystemColors.GrayText);
                    }
                    else
                    {
                        if (categroryId <= Convert.ToInt32(Categories.DefaultCategories.EndOfPredefined))
                        {
                            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                            {
                                drawBrush = new SolidBrush(Color.LightBlue);
                            }
                            else
                            {
                                drawBrush = new SolidBrush(Color.Blue);
                            }
                        }
                        else
                        {
                            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                            {
                                drawBrush = new SolidBrush(SystemColors.HighlightText);
                            }
                            else
                            {
                                drawBrush = new SolidBrush(SystemColors.WindowText);
                            }
                        }
                    }

                    e.Graphics.DrawString(categories[e.Index].Name, e.Font, drawBrush, e.Bounds);
                }
                e.DrawFocusRectangle();
            }

        }

        private void DrawText(DrawItemEventArgs e, string text)
        {
        }

        public Category SelectedCategory
        {
            get
            {
                if (this.SelectedIndex >= 0)
                {
                    return Categories.CategoryList[this.SelectedIndex];
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
